<template>
  <div>
    <roofVue title="修改密码"></roofVue>
    <van-form @submit="submit">
      <van-field
        type="password"
        label="修改密码"
        placeholder="请输入新密码"
        v-model="pwd"
        :rules="[{ required: true, message: '请输入密码' }]"
      />
      <van-field
        type="password"
        name="密码"
        label="密码"
        placeholder="再次输入密码"
        v-model="pwd1"
        :rules="[{ required: true, message: '重新输入密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="danger" native-type="submit"
          >修改</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import roofVue from "./other/roof.vue";
export default {
  data() {
    return {
      pwd: "",
      pwd1: "",
      pas: "",
    };
  },
  components: {
    roofVue,
  },
  methods: {
    async submit() {
      if (this.pwd !== this.pwd1) {
        this.pwd1 = "";
        return this.$notify({
          type: "danger",
          message: "两个密码不相等，请重新输入",
        });
      } else {
        if (this.pwd.length < 6 && this.pwd1.length < 6) {
          return this.$notify({
            type: "warning",
            message: "密码长度不能低于六位",
          });
        }
        let res = await this.$post(this.url + "user/change/password", {
          userId: this.$store.state.user.id,
          oldpassword: this.pas,
          rpassword: this.pwd,
        });
        if (res.errorCode == "0") {
          this.$toast.success("修改成功");
          this.$router.replace("/Login");
          this.Sto.Session.clear();
          location.reload();
        } else {
          this.$notify({ type: "danger", message: "登录失败" });
          this.$router.replace("/yuan");
        }
      }
    },
  },
  created() {
    this.pas = this.Sto.Session.get("pwd");
  },
};
</script>

<style  scoped>
</style>